Previous | Chapter contents | Next | Book PDF
You can use the document architecture in three general ways. The following discussion starts with the simplest and proceeds to the most complex.
The simplest way to use the document architecture is appropriate for documents that have only one window and are simple enough so that there isn't much benefit in splitting the controller layer into a model-controller and a view-controller. In this case, the developer needs only to create a subclass of NSDocument. The NSDocument subclass provides storage for the model and the ability to load and save document data. It also has any outlets and actions required for the user interface. It overrides windowNibName to return the nib file name used for documents of this type. NSDocument automatically creates an NSWindowController to manage that nib file, but the NSDocument itself serves as the nib file's "File's Owner."
If your document has only one window, but it is complex enough that you'd like to split up some of the logic in the controller layer, you can subclass NSWindowController as well as NSDocument. In this case, any outlets and actions and other behavior that is specific to the management of the user interface goes into the NSWindowController subclass. Your NSDocument subclass must override makeWindowControllers instead of windowNibName . The makeWindowControllers method should create an instance of your NSWindowController subclass and add it to the list of managed window controllers with addWindowController: . The NSWindowController should be the "File's Owner" for the nib file because this creates better separation between the view-related logic and the model-related logic. This approach is recommended for all but the most simple cases.
If your document requires multiple windows (or allows multiple windows) on a single document you should subclass NSWindowController as well as NSDocument. In your NSDocument subclass you override makeWindowControllers just as in the second procedure described above. However, in this case you might create more than one instance of NSWindowController, possibly from different subclasses of NSWindowController. Some applications need several different windows to represent one document. Therefore you probably need several different subclasses of NSWindowController and you must create one of each in makeWindowControllers . Some applications need only one window for a document but want to allow the user to create several copies of the window for a single document (sometimes this is called a multiple-view document) so that the user can have each window scrolled to a different position, or displayed in different ways. In this case, your makeWindowControllers may only create one NSWindowController, but there will be a menu command or similar control that allows the user to create others.
Previous | Chapter contents | Next | Book PDF